﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ABELGX
{
    public static class Config
    {
        public static bool Debug { get; set; } = false;
        public const DeviceType Device = DeviceType.PG;
        static string ConfigPath;
        static string UserPath;
        static string LogoPath;
        public static string DataFolder { get; private set; }
        public static string LogFolder { get; private set; }
        public static string VideoFolder { get; private set; }
        public static string VideoDayFolder { get; private set; }
        public static StringBuilder LogText { get; private set; }
        public static string WarningFolder { get; private set; }
        public static StringBuilder WarningText { get; private set; }
        public static Parameter Parameter { get; set; }
        public static UserSet UserSet { get; set; }
        public static Volume Volume { get; set; } = new Volume();
        public static void Init()
        {
            ConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.xml");
            UserPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "User.xml");
            DataFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
            if (!Directory.Exists(DataFolder))
            {
                Directory.CreateDirectory(DataFolder);
            }
            LogFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log");
            if (!Directory.Exists(LogFolder))
            {
                Directory.CreateDirectory(LogFolder);
            }
            WarningFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Warning");
            if (!Directory.Exists(WarningFolder))
            {
                Directory.CreateDirectory(WarningFolder);
            }
            VideoFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Video");
            if (!Directory.Exists(VideoFolder))
            {
                Directory.CreateDirectory(VideoFolder);
            }

            VideoDayFolder = Path.Combine(VideoFolder, DateTime.Now.ToString("yyyy-MM-dd"));
            if (!Directory.Exists(VideoDayFolder))
            {
                Directory.CreateDirectory(VideoDayFolder);
            }
            LogoPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logo.png");
            LogText = new StringBuilder(10000);
            WarningText = new StringBuilder(10000);
            ReadParameter();
            ReadUserSet();
            //SaveParameter();

            string[] vfs = Directory.GetDirectories(VideoFolder);
            foreach (var vf in vfs)
            {
                try
                {
                    string vd = Path.GetFileName(vf);
                    DateTime date = DateTime.Parse(vd);
                    if (DateTime.Now.Subtract(date).TotalDays > Parameter.Video.VideoDay)
                    {
                        Directory.Delete(vf, true);
                    }
                }
                catch
                {

                }

            }
        }
        public static ImageSource GetLogo()
        {
            if(File.Exists(LogoPath))
            {
                Bitmap bitmap = new Bitmap(LogoPath);
                MemoryStream stream = new MemoryStream();
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                bitmap.Dispose();
                ImageSourceConverter imageSourceConverter = new ImageSourceConverter();
                return (ImageSource)imageSourceConverter.ConvertFrom(stream);
            }
            return null;
        }
        public static void WriteLog(string log)
        {
            LogText.AppendLine($"{DateTime.Now:yyyy-MM-dd hh:mm:ss fff},{log}");
            if (LogText.Length > 1000)
            {
                SaveLog();
            }
        }
        public static void SaveLog()
        {
            try
            {
                if (LogText.Length <= 0)
                {
                    return;
                }
                string path = Path.Combine(LogFolder, $"{DateTime.Now:yyyy-MM-dd}.csv");
                File.AppendAllText(path, LogText.ToString());
                LogText.Clear();
            }
            catch
            {

            }
        }
        public static void WriteWarning(string log)
        {
            WarningText.AppendLine($"{DateTime.Now:yyyy-MM-dd hh:mm:ss fff},{log}");
            if (WarningText.Length > 1000)
            {
                SaveWarning();
            }
        }
        public static void SaveWarning()
        {
            try
            {
                if (WarningText.Length <= 0)
                {
                    return;
                }
                string path = Path.Combine(WarningFolder, $"{DateTime.Now:yyyy-MM-dd}.csv");
                string txt = WarningText.ToString();
                WarningText.Clear();
                File.AppendAllText(path, txt);
            }
            catch
            {

            }
        }

        public static void ReadParameter()
        {
            try
            {
                Parameter = Serialize.DeSerializeXML<Parameter>(ConfigPath);
            }
            catch(Exception ex)
            {

            }
            if (Parameter == null)
            {
                Parameter = new Parameter();
                Parameter.Init();
            }
        }
        public static void SaveParameter()
        {
            try
            {
                Serialize.SerializeXML(Parameter, ConfigPath);
            }
            catch
            {

            }
        }
        public static void ReadUserSet()
        {
            try
            {
                UserSet = Serialize.DeSerializeXML<UserSet>(UserPath);
            }
            catch (Exception ex)
            {

            }
            if (UserSet == null)
            {
                UserSet = new UserSet();
            }
        }
        public static void SaveUserSet()
        {
            try
            {
                Serialize.SerializeXML(UserSet, UserPath);
            }
            catch
            {

            }
        }
    }
    
}
